<?php
if (!isset($_SESSION['username'])) {
	header('location:setup/');
}

require('file_txt.inc.php');
getConfig('./config.ini');
$urls = getUrls('./url.ini');
$page_keyword = array_keys($urls);
$page_url = array_values($urls);
$p1size = count($urls);
$navpage1 = "跳转列表";
?>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="renderer" content="webkit" />
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
	<link rel="shortcut icon" href="favicon.ico" />
	<link rel="stylesheet" href="layui/css/layui.css">
	<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>

	<title>动态IP跳转管理</title>
</head>

<body>

	<div class="layui-container">

		<div style="display: none;">
			<button type="button" class="layui-hide" id="test1"></button>
			<textarea id="editor_id" name="v_content1" style="height:100px;"></textarea>
			<input type="text" id="img_url">
		</div>

		<div class="layui-row">
			<div class="layui-col-xs6">
				<h1>网站设置</h1>
			</div>
			<div class="layui-col-xs6" style="text-align: right;">
				<form id="upload_from" action="editor/php/upload_json.php" style="display:none" method="post" enctype="multipart/form-data">
					<input type="file" name="imgFile" value="">
				</form>
				<button class="layui-btn menu1">
					<?= $_SESSION['username'] ?>
					<i class="layui-icon layui-icon-down layui-font-12"></i>
				</button>
			</div>
		</div>


		<br>
		<form class="layui-form" action="submit.php" enctype="multipart/form-data" method="post">

			<div class="layui-tab layui-tab-card">
				<ul class="layui-tab-title">
					<li class="layui-this">网站标题设置</li>
					<li><?= $navpage1 ?></li>
				</ul>
				<div class="layui-tab-content">
					<div class="layui-tab-item layui-show" style="padding-right:20px;">
						<div class="layui-form-item">
							<label class="layui-form-label">导航标题</label>
							<div class="layui-input-block">
								<input type="text" name="title" lay-verify="title" autocomplete="off" class="layui-input" value="<?= $title ?>">
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">更新密钥</label>
							<div class="layui-input-block">
								<input type="text" name="tokey" lay-verify="title" autocomplete="off" class="layui-input" value="<?= $tokey ?>">
							</div>
						</div>	
						<div class="layui-form-item" style="color:#aaa">
							<div style="padding-left:40px">使用方法：http://你的域名?key=<b>跳转关键词</b></div>
							<br>
							<div style="padding-left:40px">更新方法：http://你的域名/update.php?tokey=<b><?=$tokey?></b>&keyword=<b>跳转关键词</b>&url=<b>跳转URL</b></div>
							<br>
							<div style="padding-left:40px">更新样例：http://你的域名/update.php?tokey=<b><?=$tokey?></b>&keyword=<b>home</b>&url=<b>http://{IP}:8088</b> 自动获取IP并更新。</div>
						</div>		
						
					</div>

					
					<div class="layui-tab-item">
						<div id="page1">
							<?php for ($i = 1; $i <= $p1size; $i++) : ?>
								<?php
									$keyword = $page_keyword[$i - 1];
									$url = $page_url[$i - 1];
								?>
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label">关键词</label>
										<div class="layui-input-inline">
											<input type="text" id="page_keyword<?= $i ?>" name="page_keyword[]" id="date" autocomplete="off" class="layui-input" value="<?= $keyword ?>">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">跳转链接</label>
										<div class="layui-input-inline" style="width:380px;">
											<input type="tel" name="page_url[]" autocomplete="off" class="layui-input" value="<?= $url ?>">
										</div>
									</div>
									<div class="layui-inline">
										<button onclick="moveup(1,<?= $i ?>)" type="button" title="向上调整顺序" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-up"></i></button>
									</div>
								</div>
							<?php endfor; ?>
						</div>
						<div class="layui-btn-container" style="text-align: center;">
							<button id="addpage1" onclick="additem('#page1',1)" type="button" class="layui-btn layui-btn-danger layui-btn-sm"><i class="layui-icon"></i></button>
						</div>

					</div>
		
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit="">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>

	<!-- 引入 layui.js 的 <script> 标签最好放置在 html 末尾 -->
	<script src="layui/layui.js"></script>
	<?php
	if (isset($_SESSION['msg'])) {
		echo '<script>  layer.msg("' . $_SESSION['msg'] . '");</script>';
		unset($_SESSION['msg']);
	}
	?>

	<script>
		layui.use(['dropdown', 'jquery', 'layer'], function() {
			var layer = layui.layer,
				form = layui.form,
				dropdown = layui.dropdown,
				element = layui.element,
				$ = layui.$ //重点处
				,
				util = layui.util;

			dropdown.render({
				elem: '.menu1',
				data: [{
					title: '修改密码',
					id: 100
				}, {
					title: '注　　销',
					id: 101
				}],
				click: function(obj) {
					if (obj.id == 100) {
						layer.open({
							type: 1,
							resize: false,
							shadeClose: true,
							area: '350px',
							title: '修改密码',
							content: ['<ul class="layui-form layui-form-pane" style="margin: 15px;">', '<li class="layui-form-item">', '<label class="layui-form-label">输入新密码</label>', '<div class="layui-input-block">', '<input type="password" class="layui-input" lay-verify="required" name="field1">', '</div>', '</li>', '<li class="layui-form-item" style="text-align:center;">', '<button type="submit" lay-submit lay-filter="*" class="layui-btn">提交</button>', '</li>', '</ul>'].join(''),
							success: function(layero, index) {
								layero.find('.layui-layer-content').css('overflow', 'visible');

								form.render().on('submit(*)', function(data) {

									layer.close(index); //关闭层
									$.ajax({
										url: "setpass.php",
										data: {
											'newpass': data.field.field1
										},
										headers: {
											'Content-Type': 'application/te;charset=utf-8'
										}, //接口json格式
										success: function(data) {
											layer.msg(data);
										},
										error: function(data) {
											layer.alert("密码修改错误。");
										}
									});
								});
							}
						});

					} else {

						window.location.href = "logout.php";

					}
				}
			});

		});

		function moveup(page_id, pos) {
			if(pos<=1) layer.msg("已经是第一项了。");
			var id = "#page" + page_id + " .layui-form-item";
			var items =  $(id).slice(pos-2, pos);	
			if(items.length!=2) return;
			inputs1 = items.eq(0).find("input");
			inputs2 = items.eq(1).find("input");

			var t1 = inputs1.eq(0).val();
			var t2 = inputs1.eq(1).val();
			var t3 = inputs1.eq(2).val();
			inputs1.eq(0).val(inputs2.eq(0).val());
			inputs1.eq(1).val(inputs2.eq(1).val());
			inputs1.eq(2).val(inputs2.eq(2).val());
			
			inputs2.eq(0).val(t1);
			inputs2.eq(1).val(t2);
			inputs2.eq(2).val(t3);
		}

		function additem(page_id, page) {
			var id = page_id + " .layui-form-item";
			var pos = $(id).length + 1;
			$(page_id).append('<div class="layui-form-item">\
							<div class="layui-inline">\
								<label class="layui-form-label">关键词</label>\
								<div class="layui-input-inline">\
									<input type="text" id="page_keyword' + pos + '" name="page_keyword[]" autocomplete="off" class="layui-input" value="">\
								</div>\
							</div>\
							<div class="layui-inline">\
								<label class="layui-form-label">跳转链接</label>\
								<div class="layui-input-inline">\
									<input type="tel" name="page_url[]" autocomplete="off" class="layui-input" value="">\
								</div>\
							</div>\
							<div class="layui-inline">\
										<button onclick="up(' + page + ',' + pos + ')" type="button" title="向上调整顺序" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-up"></i></button>\
									</div>\
						</div>');
			bindclick('#page' + page + 'up' + pos);
			
		}
	</script>

</body>

</html>